package com.ruoyi.parkinglot.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.parkinglot.mapper.ParkLaneMapper;
import com.ruoyi.parkinglot.service.ParkLaneService;
import com.ruoyi.parkinglot.utils.QRCodeGenerator;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.parkinglot.domain.ParkLane;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.Base64;
import java.util.List;

/**
* @author 31713
* @description 针对表【park_lane(车道表 - 用于管理停车场出入口车道及二维码)】的数据库操作Service实现
* @createDate 2025-02-11 13:43:44
*/
@Service
public class ParkLaneServiceImpl extends ServiceImpl<ParkLaneMapper, ParkLane>
    implements ParkLaneService {

    private static final Logger log = LoggerFactory.getLogger(ParkLaneServiceImpl.class);

    @Override
    public List<ParkLane> selectParkLaneList(ParkLane parkLane) {
        LambdaQueryWrapper<ParkLane> queryWrapper = new LambdaQueryWrapper<>();

        // 打印完整的查询参数
        log.info("开始查询车道列表");
        log.info("查询参数: parkLane = {}", parkLane);

        // 根据条件构建查询
        if (parkLane != null) {
            if (StringUtils.isNotEmpty(parkLane.getLaneName())) {
                queryWrapper.like(ParkLane::getLaneName, parkLane.getLaneName());
                log.info("添加车道名称查询条件: {}", parkLane.getLaneName());
            }
            if (StringUtils.isNotEmpty(parkLane.getLaneCode())) {
                queryWrapper.eq(ParkLane::getLaneCode, parkLane.getLaneCode());
                log.info("添加车道编码查询条件: {}", parkLane.getLaneCode());
            }
            if (parkLane.getLotId() != null) {
                queryWrapper.eq(ParkLane::getLotId, parkLane.getLotId());
                log.info("添加停车场ID查询条件: {}", parkLane.getLotId());
            }
            if (StringUtils.isNotEmpty(parkLane.getLaneType())) {
                queryWrapper.eq(ParkLane::getLaneType, parkLane.getLaneType());
                log.info("添加车道类型查询条件: {}", parkLane.getLaneType());
            }
            if (StringUtils.isNotEmpty(parkLane.getStatus())) {
                queryWrapper.eq(ParkLane::getStatus, parkLane.getStatus());
                log.info("添加状态查询条件: {}", parkLane.getStatus());
            }
        }

        // 默认只查询未删除的记录
        queryWrapper.eq(ParkLane::getDelFlag, "0");

        // 执行查询
        List<ParkLane> list = list(queryWrapper);

        // 打印查询结果信息
        if (list.isEmpty()) {
            log.warn("未查询到数据");
        } else {
            log.info("查询成功，数据条数：{}", list.size());
            log.info("第一条数据: {}", list.get(0));
        }

        return list;
    }

    @Override
    public String generateQrCode(Long laneId) {
        log.info("开始生成二维码，车道ID: {}", laneId);

        ParkLane parkLane = getById(laneId);
        if (parkLane == null) {
            log.error("车道不存在，ID: {}", laneId);
            throw new RuntimeException("车道不存在");
        }

        try {
            JSONObject content = new JSONObject();

            // 直接使用原始值，不进行转换
            content.put("laneType", parkLane.getLaneType());
            content.put("laneName", parkLane.getLaneName());
            content.put("laneCode", parkLane.getLaneCode());
            content.put("status", parkLane.getStatus());

            log.info("准备写入二维码的数据: {}", content);

            // 不进行额外的UTF-8转换，直接使用toJSONString
            String qrContent = content.toJSONString();
            log.info("最终生成的二维码内容: {}", qrContent);

            // 生成二维码并转换为Base64
            ByteArrayOutputStream stream = QRCodeGenerator.generateQRCodeImage(qrContent);
            String base64Image = "data:image/png;base64," + Base64.getEncoder().encodeToString(stream.toByteArray());

            // 更新车道的二维码链接
            parkLane.setQrCode(base64Image);
            updateById(parkLane);

            return base64Image;
        } catch (Exception e) {
            log.error("二维码生成失败，详细错误: ", e);
            throw new RuntimeException("二维码生成失败: " + e.getMessage(), e);
        }
    }
}




